/**
 * Created by dashan on 2017/3/22.
 */
var mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect('mongodb://dev1.bjvca.com:27017/test2');

var PersonSchema = new mongoose.Schema({
    firstName:String,
    lastName:String,
    createDate:Date,
    time1:Date,
    time2:Date,
});


/*
 类型：

 文档中间件（init ,validate,save,remove）,
 查询中间件（count/find/findOne/findOneAndRemove/findOneAndUpdate/update）

 按类型又分为：

 预处理中间件
 后置处理中间件
 */



PersonSchema.pre('save',function(next){
    this.time1 = new Date();
    this.time2 = Date.now();
    console.log("pre save 111111111111111");
    next();
});

function test1(){
    console.log("this is test11111");
}
//
PersonSchema.pre('save',true,function(next,done){
    this.time1 = new Date();
    this.time2 = Date.now();
    console.log("pre save 22222222222222222");
    done();
    next();
});


PersonSchema.post('find',function(doc){

    // this.lastName = this.lastName+"222";
    console.log('+++++++++++');
    console.log(doc);
    console.log('+++++++++++');
    // next();
});


var Person = mongoose.model('Person',PersonSchema)

var person = new Person({
    firstName:'Sid',
    lastName:'chen'
})

person.save(function(err){
    if(err){
        console.log(err);
        return;
    }
    console.log("success!");
})


// Person.find({},function(err,docs){
//     console.log("==================== begin find list ====================");
//     console.log(err);
//     console.log(docs);
// });

